package cn.cheegon.blog.controller;

import cn.cheegon.blog.common.api.CommonPage;
import cn.cheegon.blog.common.api.CommonResult;
import cn.cheegon.blog.entity.dto.MessageDto;
import cn.cheegon.blog.entity.req.AddMessageReq;
import cn.cheegon.blog.entity.req.EditMessageReq;
import cn.cheegon.blog.entity.rsp.MessageListRsp;
import cn.cheegon.blog.model.CmsComment;
import cn.cheegon.blog.model.CmsMessage;
import cn.cheegon.blog.service.CmsMessageService;
import cn.hutool.core.bean.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Desc:
 * @Author: cheegon
 * @Date: Create in 2024/6/28  上午9:21
 */
@RestController
@RequestMapping("/admin/message/*")
public class AdminMessageController extends AdminController {
    @Autowired
    private CmsMessageService cmsMessageService;

    @GetMapping("list")
    public CommonResult<CommonPage<MessageListRsp>> getList(@RequestParam(name = "status", required = false) Integer status,
                                                            @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                                            @RequestParam(name = "pageSize", defaultValue = "20") Integer pageSize) {
        return CommonResult.success(cmsMessageService.getAdminList(status, pageNum, pageSize));
    }

    @GetMapping("updateStatus")
    public CommonResult updateStatus(@RequestParam(name = "id") Long id,
                                     @RequestParam(name = "status") Integer status) {
        CmsMessage dto = new CmsMessage();
        dto.setId(id);
        dto.setStatus(status);
        cmsMessageService.edit(dto);
        return CommonResult.success();
    }

    @PostMapping("add")
    public CommonResult addMessage(@RequestBody AddMessageReq req) {
        cmsMessageService.add(BeanUtil.toBean(req, CmsMessage.class));
        return CommonResult.success();
    }

    @PostMapping("edit")
    public CommonResult editMessage(@RequestBody EditMessageReq req) {
        cmsMessageService.edit(BeanUtil.toBean(req, CmsMessage.class));
        return CommonResult.success();
    }

    @GetMapping("del/{id}")
    public CommonResult delMessage(@PathVariable Long id) {
        cmsMessageService.del(id);
        return CommonResult.success();
    }
}
